import 'package:json_annotation/json_annotation.dart';

part 'auth_models.g.dart';

/// 用户信息模型
@JsonSerializable()
class UserInfo {
  const UserInfo({
    required this.id,
    required this.username,
    required this.phoneNumber,
    this.email,
    this.avatar,
    this.status,
    this.role,
    this.nickname,
    this.createdAt,
    this.updatedAt,
  });

  @JsonKey(fromJson: _parseId)
  final int id;
  final String username;
  @JsonKey(name: 'phone_number')
  final String phoneNumber;
  final String? email;
  final String? avatar;
  final String? status;
  final String? role;
  final String? nickname;
  @JsonKey(name: 'created_at')
  final DateTime? createdAt;
  @JsonKey(name: 'updated_at')
  final DateTime? updatedAt;

  factory UserInfo.fromJson(Map<String, dynamic> json) => _$UserInfoFromJson(json);
  Map<String, dynamic> toJson() => _$UserInfoToJson(this);

  @override
  String toString() => 'UserInfo(id: $id, username: $username, phoneNumber: $phoneNumber)';

  // 处理后端返回的字符串ID
  static int _parseId(dynamic value) {
    if (value is String) {
      return int.parse(value);
    }
    return value as int;
  }
}

/// 发送验证码请求
@JsonSerializable()
class SendCodeRequest {
  const SendCodeRequest({required this.phoneNumber});

  @JsonKey(name: 'phone_number')
  final String phoneNumber;

  factory SendCodeRequest.fromJson(Map<String, dynamic> json) => _$SendCodeRequestFromJson(json);
  Map<String, dynamic> toJson() => _$SendCodeRequestToJson(this);
}

/// 发送验证码响应
@JsonSerializable()
class SendCodeResponse {
  const SendCodeResponse({
    required this.verificationId,
    required this.expiresIn,
  });

  @JsonKey(name: 'verification_id')
  final String verificationId;
  @JsonKey(name: 'expires_in')
  final int expiresIn;

  factory SendCodeResponse.fromJson(Map<String, dynamic> json) => _$SendCodeResponseFromJson(json);
  Map<String, dynamic> toJson() => _$SendCodeResponseToJson(this);
}

/// 验证码登录请求
@JsonSerializable()
class VerifyCodeRequest {
  const VerifyCodeRequest({
    required this.phoneNumber,
    required this.code,
    required this.verificationId,
  });

  @JsonKey(name: 'phone_number')
  final String phoneNumber;
  final String code;
  @JsonKey(name: 'verification_id')
  final String verificationId;

  factory VerifyCodeRequest.fromJson(Map<String, dynamic> json) => _$VerifyCodeRequestFromJson(json);
  Map<String, dynamic> toJson() => _$VerifyCodeRequestToJson(this);
}

/// 登录响应数据
@JsonSerializable()
class LoginResponse {
  const LoginResponse({
    required this.user,
    required this.token,
    this.refreshToken,
    required this.expiresIn,
  });

  final UserInfo user;
  final String token;
  @JsonKey(name: 'refresh_token')
  final String? refreshToken;
  @JsonKey(name: 'expires_in')
  final int expiresIn;

  factory LoginResponse.fromJson(Map<String, dynamic> json) => _$LoginResponseFromJson(json);
  Map<String, dynamic> toJson() => _$LoginResponseToJson(this);
}

/// 刷新Token请求
@JsonSerializable()
class RefreshTokenRequest {
  const RefreshTokenRequest({required this.refreshToken});

  @JsonKey(name: 'refresh_token')
  final String refreshToken;

  factory RefreshTokenRequest.fromJson(Map<String, dynamic> json) => _$RefreshTokenRequestFromJson(json);
  Map<String, dynamic> toJson() => _$RefreshTokenRequestToJson(this);
}

